using FineCollection.Intf;
using FineCollection.Test;
using NUnit.Framework;




namespace FineCollection.Const
{
    [TestFixture]
    public class ConstSortSet_test
    {

        [Test]
        public void Test_ConstructorSort1()
        {
            ConstSortSet<byte> s1 = CC1.SortSetOf<byte>(11);

            Assert.IsTrue(s1.IsNotEmpty);
            Assert.IsFalse(s1.IsEmpty);
            Assert.AreEqual(1, s1.Count);

            Assert.AreEqual(11, s1.First);
            Assert.AreEqual(11, s1.Last);

            Assert.IsTrue(s1.Contains(11));
            Assert.IsTrue(s1.Contains(11));
        }


        [Test]
        public void Test_ConstructorSort2()
        {
            ConstSortSet<byte> s1 = CC1.SortSetOf<byte>(77,33);

            Assert.IsTrue(s1.IsNotEmpty);
            Assert.IsFalse(s1.IsEmpty);
            Assert.AreEqual(2, s1.Count);

            Assert.AreEqual(33, s1.First);
            Assert.AreEqual(77, s1.Last);

            Assert.IsTrue(s1.Contains(33));
            Assert.IsTrue(s1.Contains(77));
        }


        [Test]
        public void Test_ConstructorSort3()
        {
            ConstSortSet<byte> s3 = CC1.SortSetOf<byte>(55, 77, 33);

            Assert.IsTrue(s3.IsNotEmpty);
            Assert.IsFalse(s3.IsEmpty);
            Assert.AreEqual(3, s3.Count);

            Assert.AreEqual(33, s3.First);
            Assert.AreEqual(77, s3.Last);

            Assert.AreEqual(33, s3[1]);
            Assert.AreEqual(55, s3[2]);
            Assert.AreEqual(77, s3[3]);

            Assert.IsTrue(s3.Contains(33));
            Assert.IsTrue(s3.Contains(55));
            Assert.IsTrue(s3.Contains(77));
        }


        [Test]
        public void Test_ConstructorSort5()
        {
            byte[] arr5 = new byte[] { 44, 22, 11, 55, 33 };
            ConstSortSet<byte> s5 = CC1.SortSetOf<byte>(arr5);

            Assert.AreEqual(5, s5.Count);

            Assert.AreEqual(11, s5[1]);
            Assert.AreEqual(22, s5[2]);
            Assert.AreEqual(33, s5[3]);
            Assert.AreEqual(44, s5[4]);
            Assert.AreEqual(55, s5[5]);

            Assert.IsTrue(s5.Contains(11));
            Assert.IsTrue(s5.Contains(22));
            Assert.IsTrue(s5.Contains(33));
            Assert.IsTrue(s5.Contains(44));
            Assert.IsTrue(s5.Contains(55));
        }


        [Test]
        public void Test_Duplicates3()
        {

            var set = CC1.SortSetOf(111L, 333L, 333L, 333L, 333L, 555L);

            set.Contains(111L)._true_();
            set.Contains(333L)._true_();
            set.Contains(555L)._true_();

            long[] array = set.ToArray();

            array._array_(111L, 333L, 555L);

            set.Count._equals_(3);
        }


        [Test]
        public void Test_DuplicatesAtTheBegin()
        {

            var set = CC1.SortSetOf(111L, 111L, 111L, 333L, 555L);

            set.Contains(111L)._true_();
            set.Contains(333L)._true_();
            set.Contains(555L)._true_();

            long[] array = set.ToArray();

            array._array_(111L, 333L, 555L);
        }


        [Test]
        public void Test_DuplicatesAtTheEnd()
        {

            var set = CC1.SortSetOf(111L, 333L, 555L, 555L, 555L);

            set.Contains(111L)._true_();
            set.Contains(333L)._true_();
            set.Contains(555L)._true_();

            long[] array = set.ToArray();

            array._array_(111L, 333L, 555L);
        }



        [Test]
        public void Test_Find_existentItem()
        {
            var set = CC1.SortSetOf(111L,222L,333L,444L,555L);

            bool found;
            int position;

            set.Find(111L, out found, out position);
            found     ._true_();
            position  ._equals_(1);

            set.Find(222L, out found, out position);
            found._true_();
            position._equals_(2);

            set.Find(333L, out found, out position);
            found._true_();
            position._equals_(3);

            set.Find(444L, out found, out position);
            found._true_();
            position._equals_(4);

            set.Find(555L, out found, out position);
            found._true_();
            position._equals_(5);
        }


        [Test]
        public void Test_Find_nearestItem()
        {
            var set = CC1.SortSetOf(111L,222L,333L,444L,555L);

            bool found;
            int position;

            set.Find(300L, out found, out position);
            found     ._false_();
            position  ._equals_(3);
        }


        [Test]
        public void Test_Find_nearestItemBeforeBegin()
        {
            var set = CC1.SortSetOf(111L,222L);

            bool found;
            int position;

            set.Find(1L, out found, out position);
            found     ._false_();
            position  ._equals_(1);
        }


        [Test]
        public void Test_Find_nearestItemAfterEnd()
        {
            var set = CC1.SortSetOf(111L,222L);

            bool found;
            int position;

            set.Find(1000L, out found, out position);
            found     ._false_();
            position  ._equals_(3);
        }


        [Test]
        public void Test_Find_emptySet()
        {
            var set = CC1.SortSetOf<long>();

            bool found;
            int position;

            set.Find(1L, out found, out position);
            found     ._false_();
            position  ._equals_(1);
        }


        [Test]
        [ExpectedException(typeof(NullItemsException))]
        public void Test_Find_null()
        {
            var set = CC1.SortSetOf("aaa","bbb");

            bool found;
            int position;

            set.Find(null, out found, out position);
        }


        [Test]
        public void Test_IndexOf_positive()
        {
            var set = CC1.SortSetOf("balalaika", "guitar", "zurna");
            int position = set.IndexOf("GuitaR".ToLower());

            position ._equals_(2);
        }


        [Test]
        public void Test_IndexOf_positive_same()
        {
            string guitar = "guitar";

            var set = CC1.SortSetOf("balalaika","guitar","zurna");
            int position = set.IndexOf(guitar);

            position ._equals_(2);
        }


        [Test]
        public void Test_IndexOf_negative()
        {
            var set = CC1.SortSetOf("balalaika","guitar","zurna");
            int position = set.IndexOf("duduk");

            position ._equals_(int.MinValue);
        }


        [Test]
        public void Test_IndexOf_emptySet()
        {
            var set = CC1.SortSetOf<string>();
            int position = set.IndexOf("trumpet");

            position ._equals_(int.MinValue);
        }


        [Test]
        public void Test_IndexOf_null()
        {
            var set = CC0.SortSetOf("balalaika","guitar","zurna");
            int position = set.IndexOf((string)null);

            position ._equals_(int.MinValue);
        }


        [Test]
        public void Test_Intersect_1()
        {
            var setA = CC1.SortSetOf("111","222","333");
            var setB = CC1.SortSetOf("222","333","444");

            var setC = setA * setB;

            setC                 ._is_not_null_();
            setC.count           ._equals_(2);
            setC.Contains("111") ._false_();
            setC.Contains("222") ._true_();
            setC.Contains("333") ._true_();
            setC.Contains("444") ._false_();

            setC.FirstIndex._equals_(setA.FirstIndex);
        }


        [Test]
        public void Test_Union_1()
        {
            var setA = CC1.SortSetOf("AAA", "aaa");
            var setB = CC1.SortSetOf("BBB", "bbb");

            var setC = setA + setB;

            setC                  ._is_not_null_();
            setC.count            ._equals_(4);
            setC.Contains("AAA")  ._true_();
            setC.Contains("aaa")  ._true_();
            setC.Contains("BBB")  ._true_();
            setC.Contains("bbb")  ._true_();

            setC.FirstIndex       ._equals_(setA.FirstIndex);
        }


        [Test]
        public void Test_Union_2()
        {
            var setA = CC1.SortSetOf("aaa", "xxx");
            var setB = CC1.SortSetOf("bbb", "xxx");

            var setC = setA + setB;

            setC                  ._is_not_null_();
            setC.count            ._equals_(3);
            setC.Contains("aaa")  ._true_();
            setC.Contains("bbb")  ._true_();
            setC.Contains("xxx")  ._true_();
        }


        [Test]
        public void Test_Minus_1()
        {
            var setA = CC1.SortSetOf("aaa","bbb","ccc");
            var setB = CC1.SortSetOf("bbb","ddd");

            var setC = setA - setB;

            setC.                 _is_not_null_();
            setC.count.           _equals_(2);
            setC.Contains("aaa"). _true_();
            setC.Contains("bbb"). _false_();
            setC.Contains("ccc"). _true_();
            setC.Contains("ddd"). _false_();

            setC.FirstIndex._equals_(setA.FirstIndex);
        }






    }
}